import asyncio
import subprocess


async def run_js_file(js_file_path):
    """
    异步执行一个JavaScript文件
    """
    try:
        process = await asyncio.create_subprocess_exec(
            'node',  # 假设使用Node.js执行JavaScript，若不同需替换
            js_file_path,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE
        )

        stdout, stderr = await process.communicate()

        if process.returncode == 0:
            print(f"JavaScript file {js_file_path} executed successfully")
            print("标准输出:", stdout.decode('utf-8'))
        else:
            print(f"JavaScript file {js_file_path} execution failed")
            print("错误输出:", stderr.decode('utf-8'))
    except Exception as e:
        print(f"Error running JavaScript file {js_file_path}: {e}")


async def main():
    """
    主函数，并发执行多个JavaScript文件
    """
    js_files = ['script1.js', 'script2.js', 'script3.js']  # 替换为实际的JavaScript文件路径列表
    tasks = [run_js_file(js_file) for js_file in js_files]
    await asyncio.gather(*tasks)


if __name__ == "__main__":
    asyncio.run(main())
